#define _CRT_SECURE_NO_WARNINGS

class Solution {
public:
    int eraseOverlapIntervals(vector<vector<int>>& intervals) {
        if (intervals.size() == 0)
            return 0;
        sort(intervals.begin(), intervals.end());
        int ret = 0;
        int end = intervals[0][1];
        for (int i = 1; i < intervals.size(); i++)
        {
            if (intervals[i][0] < end)
            {
                ret++;
                end = (intervals[i][1] > end) ? end : intervals[i][1];
            }
            else
                end = intervals[i][1];
        }
        return ret;
    }
};

class Solution {
public:
    vector<int> partitionLabels(string s) {
        vector<int> ret;
        unordered_map<char, int> um;
        int begin = 0;
        int end = 0;
        for (int i = 0; i < s.size(); i++)
            um[s[i]] = i;
        for (int i = 0; i < s.size(); i++)
        {
            if (i < end)
            {
                end = (end > um.find(s[i])->second + 1) ? end : um.find(s[i])->second + 1;
                ret[ret.size() - 1] = end - begin;
            }
            else if (end < um.find(s[i])->second + 1)
            {
                begin = end;
                end = um.find(s[i])->second + 1;
                ret.push_back(end - begin);
            }
        }
        return ret;
    }
};

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        vector<vector<int>> ret;
        sort(intervals.begin(), intervals.end());
        for (int i = 0; i < intervals.size(); i++)
        {
            if (i == 0)
                ret.push_back(intervals[i]);
            else
            {
                if (ret[ret.size() - 1][1] >= intervals[i][0])
                {
                    int end = (ret[ret.size() - 1][1] > intervals[i][1]) ? ret[ret.size() - 1][1] : intervals[i][1];
                    int begin = ret[ret.size() - 1][0];
                    ret.pop_back();
                    ret.push_back({ begin,end });
                }
                else
                    ret.push_back(intervals[i]);
            }
        }
        return ret;
    }
};